﻿using DotNetCore.MyBlogApi.Data.Attributes;
using DotNetCore.MyBlogApi.Data.Models;
using DotNetCore.MyBlogApi.Data.Models.m2m_middle;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetCore.MyBlogApi.Data
{
    public class ApplicationSqlServerContext : DbContext
    {
        public ApplicationSqlServerContext(DbContextOptions<ApplicationSqlServerContext> options)
: base(options)
        {
        }

        public DbSet<SysUser> SysUser { get; set; }
        public DbSet<Article> Article { get; set; }
        public DbSet<ArticleCategory> ArticleCategory { get; set; }
        public DbSet<ArticleTag> ArticleTag { get; set; }
        public DbSet<Article_ArticleTag> Article_ArticleTag { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            #region Article_ArticleTag 多对多关系配置
            builder.Entity<Article_ArticleTag>()
                .HasKey(t => new { t.ArticleId, t.TagId });
            builder.Entity<Article_ArticleTag>()
                .HasOne(aa => aa.Article);
            //.WithMany(a => a.Article_ArticleTags)
            //.HasForeignKey(pt => pt.ArticleId);
            builder.Entity<Article_ArticleTag>()
                .HasOne(aa => aa.Tag)
                .WithMany(at => at.Article_ArticleTags)
                .HasForeignKey(pt => pt.TagId);
            #endregion

            base.OnModelCreating(builder);

        }
    }
    public static class ProjectExtensions
    {
        public static void SeedDB(this ApplicationSqlServerContext context)
        {
            var updater = new DBDescriptionUpdater<ApplicationSqlServerContext>(context);
            updater.UpdateDatabaseDescriptions();
        }

    }
}
